package com.yunhe.policyfulfillment.authority.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.authority.domain.PolicyUser;
import com.yunhe.policyfulfillment.authority.model.DisableRecoveryUser;
import com.yunhe.policyfulfillment.authority.model.PolicyUserChangePassword;
import com.yunhe.policyfulfillment.authority.model.PolicyUserQueryCondition;
import com.yunhe.policyfulfillment.authority.service.IPolicyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName RestPolicyUserController
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/22 16:58
 * @Version 1.0
 **/
@Api(value = "PolicyUser", description = "政策兑现后台用户", tags = {"Model_Policy_User"})
@RestController
@RequestMapping(value = "/policyUsers")
public class PolicyUserController {

    @Autowired
    private IPolicyUserService policyUserService;


    /**
     * 查询所有用户
     * @author hetao
     * @date 2021/07/22
     **/
    @ApiOperation("查询所有用户")
    @GetMapping(value = "/findPolicyUsers")
    public ResponseEntity<ResultObject<List<PolicyUser>>> findPolicyUsers(){
        return new ResponseEntity<>(new ResultObject(policyUserService.findPolicyUsers()), HttpStatus.OK) ;
    }

    /**
     * 新增
     * @author misty
     * @date 2021/09/17
     **/
    @ApiOperation("新增政策兑现后台用户")
    @PostMapping("/createPolicyUser")
    public ResponseEntity<ResultObject> createPolicyUser(@RequestBody PolicyUser policyUser) throws ArgumentErrorException {
        policyUserService.createPolicyUser(policyUser);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("根据条件查询用户")
    @PostMapping("/findPolicyUserByCondition")
    public ResponseEntity<ResultObject<List<PolicyUser>>> findPolicyUserByCondition(@RequestBody PolicyUserQueryCondition policyUserQueryCondition) throws ArgumentErrorException {
        PageResult pageResult = policyUserService.findPolicyUserByCondition(policyUserQueryCondition);
        return new ResponseEntity(new ResultObject<>(pageResult), HttpStatus.OK);
    }

    /**
     * 重置密码
     * @param ids 用户ID
     */
    @ApiOperation("重置密码")

    @PostMapping(value = "/resetPassword")
    public ResponseEntity<ResultObject<String>> resetPassword(@RequestBody List<Long> ids) throws ArgumentErrorException {

        return new ResponseEntity(new ResultObject<>(policyUserService.resetPassword(ids)), HttpStatus.OK);
    }

    @ApiOperation("禁用/恢复账号")
    @PostMapping(value = "/disableRecoveryUser")
    public ResponseEntity<ResultObject> disableRecoveryUser (@RequestBody DisableRecoveryUser disableRecoveryUser) throws ArgumentErrorException {
        policyUserService.disableRecoveryUser(disableRecoveryUser);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    /**
     * 刪除
     * @author misty
     * @date 2021/09/17
     **/
    @ApiOperation("根据id删除政策兑现后台用户")
    @DeleteMapping("/deletePolicyUser")
    public ResponseEntity<ResultObject> deletePolicyUser(@RequestBody List<Long> ids){
        policyUserService.deletePolicyUser(ids);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    /**
     * 更新
     * @author misty
     * @date 2021/09/17
     **/
    @ApiOperation("更新政策兑现后台用户")
    @PostMapping("/updatePolicyUser")
    public ResponseEntity<ResultObject> updatePolicyUser(@RequestBody PolicyUser policyUser) throws ArgumentErrorException {
        policyUserService.updatePolicyUser(policyUser);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    /**
     * 查询 根据主键 id 查询
     * @author misty
     * @date 2021/09/17
     **/
    @ApiOperation("根据id查询政策兑现后台用户")
    @GetMapping("/findPolicyUserById")
    public ResponseEntity<ResultObject<PolicyUser>> findPolicyUserById(@RequestParam(value = "id",required = true) int id){
        return new ResponseEntity<>(new ResultObject(policyUserService.findPolicyUserById(id)),HttpStatus.OK);
    }

    @ApiOperation("修改密码")
    @PostMapping("/updatePassword")
    public ResponseEntity<ResultObject<PolicyUser>> updatePassword(@RequestBody PolicyUserChangePassword policyUserChangePassword) throws ArgumentErrorException {
        policyUserService.updatePassword(policyUserChangePassword);
        return new ResponseEntity<>(new ResultObject(),HttpStatus.OK);
    }

    @ApiOperation("根据角色查询绑定的用户")
    @PostMapping("/findPolicyUsersByRoleId")
    public ResponseEntity<ResultObject<List<PolicyUser>>> findPolicyUsersByRoleId(@RequestParam(value = "roleId") Long roleId) {
        List<PolicyUser> policyUserList = policyUserService.findPolicyUsersByRoleId(roleId);
        return new ResponseEntity<>(new ResultObject(policyUserList),HttpStatus.OK);
    }

    @ApiOperation("后台用户解绑角色")
    @PostMapping("/unbindRole")
    public ResponseEntity<ResultObject> unbindRole(@RequestParam(value = "id") Long id) {
        policyUserService.unbindRole(id);
        return new ResponseEntity<>(new ResultObject(),HttpStatus.OK);
    }
}
